package com.fhnew.app.ui.activity.recomend;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;

import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.ui.activity.recomend.fragment.CommissionlistFragment;
import com.fhnew.app.ui.activity.recomend.fragment.MycommissionFragment;
import com.fhnew.app.ui.activity.recomend.fragment.MyrecommendFragment;
import com.fhnew.app.ui.activity.recomend.fragment.RecommenddetailsFragment;
import com.fhnew.app.ui.activity.recomend.fragment.TeamListFragment;
import com.fhnew.app.ui.activity.recomend.fragment.TeamtableFragment;
import com.fhnew.app.util.EventBus.ReloginEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * 推荐
 */
public class RecommendActivity extends AppCompatActivity {

    private ImageView reback;
    private ImageView shezhi;
    private TextView myrecommend;
    private RelativeLayout qipailayout;
    private TextView mycommission;
    private RelativeLayout buyulayout;
    private TextView commissionlist;
    private TextView teamtable;
    private RelativeLayout dianjinglayout;
    private TextView recommenddetails;
    private RelativeLayout dianzilayout;
    private FrameLayout content;
    private RelativeLayout teamlistlayout;
    private TextView teamlistbt;
    private Fragment fmyrecommend = new MyrecommendFragment();//我的推荐
    private Fragment fmycommission = new MycommissionFragment();//我的佣金
    private Fragment fcommissionlist = new CommissionlistFragment();//佣金记录
    private Fragment fteamtable = new TeamtableFragment();//团队报表
    private Fragment frecommendde = new RecommenddetailsFragment();//推荐介绍
    private Fragment teamListFragment = new TeamListFragment();//团队列表

    @Override
    protected void onPause () {
        super.onPause();
    }


    private void initView () {
        reback = findViewById(R.id.reback);
        shezhi = findViewById(R.id.shezhi);
        myrecommend = findViewById(R.id.myrecommend);
        qipailayout = findViewById(R.id.qipailayout);
        mycommission = findViewById(R.id.mycommission);
        buyulayout = findViewById(R.id.buyulayout);
        commissionlist = findViewById(R.id.commissionlisttext);
        teamtable = findViewById(R.id.teamtable);
        dianjinglayout = findViewById(R.id.dianjinglayout);
        recommenddetails = findViewById(R.id.recommenddetails);
        dianzilayout = findViewById(R.id.dianzilayout);
        content = findViewById(R.id.content);
        teamlistlayout = findViewById(R.id.teamlistlayout);
        teamlistbt = findViewById(R.id.teamlistbt);


        teamlistlayout.setOnClickListener(onClickListener);
        reback.setOnClickListener(onClickListener);
        shezhi.setOnClickListener(onClickListener);
        myrecommend.setOnClickListener(onClickListener);
        mycommission.setOnClickListener(onClickListener);
        commissionlist.setOnClickListener(onClickListener);
        teamtable.setOnClickListener(onClickListener);
        recommenddetails.setOnClickListener(onClickListener);

    }

    private void initfragment () {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (!fmyrecommend.isAdded()) {
            ft.add(R.id.content, fmyrecommend, "1");
        }
        if (!fmycommission.isAdded()) {
            ft.add(R.id.content, fmycommission, "2");
        }
        if (!fcommissionlist.isAdded()) {
            ft.add(R.id.content, fcommissionlist, "3");
        }
        if (!fteamtable.isAdded()) {
            ft.add(R.id.content, fteamtable, "4");
        }
        if (!frecommendde.isAdded()) {
            ft.add(R.id.content, frecommendde, "5");
        }
        if (!teamListFragment.isAdded()) {
            ft.add(R.id.content, teamListFragment, "6");
        }
        ft.commitAllowingStateLoss();
        changeFragmemt("1");
    }

    private void changeFragmemt (String s) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (s) {
            case "1":
                ft.show(fmyrecommend);
                ft.hide(fmycommission);
                ft.hide(fcommissionlist);
                ft.hide(fteamtable);
                ft.hide(frecommendde);
                ft.hide(teamListFragment);
                ft.commitAllowingStateLoss();
                break;
            case "2":
                ft.hide(fmyrecommend);
                ft.show(fmycommission);
                ft.hide(fcommissionlist);
                ft.hide(fteamtable);
                ft.hide(frecommendde);
                ft.hide(teamListFragment);
                ft.commitAllowingStateLoss();
                break;
            case "3":
                ft.hide(fmyrecommend);
                ft.hide(fmycommission);
                ft.show(fcommissionlist);
                ft.hide(fteamtable);
                ft.hide(frecommendde);
                ft.hide(teamListFragment);
                ft.commitAllowingStateLoss();
                break;
            case "4":
                ft.hide(fmyrecommend);
                ft.hide(fmycommission);
                ft.hide(fcommissionlist);
                ft.show(fteamtable);
                ft.hide(frecommendde);
                ft.hide(teamListFragment);
                ft.commitAllowingStateLoss();
                break;
            case "5":
                ft.hide(fmyrecommend);
                ft.hide(fmycommission);
                ft.hide(fcommissionlist);
                ft.hide(fteamtable);
                ft.show(frecommendde);
                ft.hide(teamListFragment);
                ft.commitAllowingStateLoss();
                break;
            case "6":
                ft.hide(fmyrecommend);
                ft.hide(fmycommission);
                ft.hide(fcommissionlist);
                ft.hide(fteamtable);
                ft.hide(frecommendde);
                ft.show(teamListFragment);
                ft.commitAllowingStateLoss();
                break;
        }
    }

    private void layoutview () {
        myrecommend.setBackgroundResource(R.mipmap.gameup);
        mycommission.setBackgroundResource(R.mipmap.gameup);
        commissionlist.setBackgroundResource(R.mipmap.gameup);
        teamtable.setBackgroundResource(R.mipmap.gameup);
        recommenddetails.setBackgroundResource(R.mipmap.gameup);
        teamlistbt.setBackgroundResource(R.mipmap.gameup);
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏

        setContentView(R.layout.activity_recommend);

        initView();
        initInfo();

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initfragment();
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.reback:
                    finish();
                    break;
                case R.id.shezhi:
                    break;
                case R.id.myrecommend:
                    changeFragmemt("1");
                    layoutview();
                    myrecommend.setBackgroundResource(R.mipmap.gamedown);
                    break;
                case R.id.mycommission:
                    changeFragmemt("2");
                    layoutview();
                    mycommission.setBackgroundResource(R.mipmap.gamedown);
                    break;
                case R.id.commissionlisttext:
                    changeFragmemt("3");
                    layoutview();
                    commissionlist.setBackgroundResource(R.mipmap.gamedown);
                    break;
                case R.id.teamtable:
                    changeFragmemt("4");
                    layoutview();
                    teamtable.setBackgroundResource(R.mipmap.gamedown);
                    break;
                case R.id.recommenddetails:
                    changeFragmemt("5");
                    layoutview();
                    recommenddetails.setBackgroundResource(R.mipmap.gamedown);
                    break;
                case R.id.teamlistlayout:
                    changeFragmemt("6");
                    layoutview();
                    teamlistbt.setBackgroundResource(R.mipmap.gamedown);
                    break;
            }
        }
    };

    @Override
    protected void onDestroy () {
        super.onDestroy();
        userModel.unRegistListener(userInformationChanged);
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event (ReloginEvent reloginEvent) {
        if (reloginEvent.getMessage().equals("100")) {
            changeFragmemt("4");
            layoutview();
            teamtable.setBackgroundResource(R.mipmap.gamedown);
        }
    }

    private UserModel.UserInformationChanged userInformationChanged =new UserModel.UserInformationChanged() {
        @Override
        public void userInformationChanged () {
            if(!userModel.isLogin()){
                Toast.makeText(RecommendActivity.this, "登录失效", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    };


    private UserModel userModel;

    private void initInfo(){
        userModel = UserModel.getInstance(this);
        userModel.registListener(userInformationChanged);
    }


}
